baselib.report(0x00, "TD4.7.1 beginning.")
-- 下面是初始化操作:
busilib.SetOrientation(1)              -- 设置USB的CC线,另一根则为Vconn线(这里设置CC1)
busilib.SetRpValue(1, 0)               -- 设置USB Source用于标识输出电流能力的电阻值,第二个参数写0代表500mA
-- 第一步:CVS transitions to Unattached.SRC
busilib.setUsbCcs(CCS.Unattached)     
busilib.setUsbRole(ROLE.Source)
-- 第二步:CVS waits 5ms
baselib.delay(5)
-- 第三步:For a PUT_R, CVS terminates one CC pin with Ra in any state other than Disabled for the remainder of the test
-- 对于PUT_R，CVS在测试的剩余时间内在任何状态下都有一个CC引脚提供Ra（除了Disabled状态）
if PUT_R then
    busilib.SetSwitchState(SW.CC2_RA, 1)    -- 将CC2_RA开关打开
end
-- 第四步:CVS verifies:
-- 4.a步:PUT presents Rd within max dcSRC.DRP x max tDRP [TD.4.7.1.V.1]
-- PUT在max dcSRC.DRP x max tDRP时间内提供Rd，意思应该是在这段时间后，PUT应该提供了Rd
baselib.delay(70)                      -- 等待max dcSRC.DRP(0.7) x max tDRP(100ms)
local VCC1 = busilib.GetADC(ADC.CC1, 4)
baselib.report(0x00, "CC1 is " .. VCC1 .. "mV")
if VCC1 < 200 then
    baselib.report(0x00, "TD4.7.1.V.1 pass.")
else
    baselib.report(0x00, "TD4.7.1.V.1 fail.")
end
-- 4.b步:PUT transitions from Rp to Rd within tDRPTransition [TD.4.7.1.V.2]
-- 不理解这个步骤
-- 4.c步:PUT applies Rd for tCCDebounce [TD.4.7.1.V.3]
--[[从T1+100ms 开始,开始连续N次(暂定N=5,间隔1ms)检测CC1电压处于0.25v - 1.50v之间的,并设置超时时间为100ms :
(1)若检测到连续5次满足,上报TD.4.5.6.V3为PASS
(2)若在100ms都不能连续5次满足,上报TD.4.5.6.V3为FAIL,同时上报最后一次检测到的CC1电压]]
local waitResult = busilib.waitCondition4ad(ADC.CC1, 4, 200, 1600, 100, 200)
local Tfour = baselib.get_time()
local Tpresent = math.floor((Tfour - Tone) / 1000)
if waitResult == LuaOk then
    baselib.report(0x00, "Rd is presented for at least 100ms within " .. Tpresent .. "ms.")
    baselib.report(0x00, "TD4.7.1.V.3 pass.")
else
    baselib.report(0x00, "Rd presentation timeout.")
    baselib.report(0x00, "TD4.7.1.V.3 fail.")
end
-- 第五步:CVS transitions to Attached.SRC
busilib.setUsbCcs(CCS.Attached)
-- 第六步:CVS verifies that PUT transitions to Attached.SNK:      验证PUT转换到Attached.SNK状态
--[[6.a步:监测VBUS电流,for 500ms,电流数值可以是20ms内的平均值,判别不超过500mA(对应tester宣传的500mA供电能力)         
(原文:PUT sinks current according to CVS advertisement. [TD.4.7.1.V.4]) ]]
local trans1 = busilib.newTrans4ad(4, ADC.CC2, {2750})
--这里对上面的trans1的状态1监控,6.d的这两行代码写前面是因为需要提前启动监控器
local monitor3 = busilib.newMonitor(TMD.CHECK_POINT, trans1, {1}, {0}, {0})
if busilib.waitCondition4ad(ADC.VBUS_I, 20, 500, 5000, 1, 500) == LuaOk then
    baselib.report(0x00, "TD.4.7.1.V.4 fail.")         --出现LuaOK说明有大于500mA的电流出现了一次,检查点fail
else
    baselib.report(0x00, "TD.4.7.1.V.4 pass.")
end
-- 6.b步:If PUT supports PD:PUT and CVS negotiate an explicit contract. [TD.4.7.1.V.5]
-- 如果PUT支持PD，CVS和PUT协商PD协议
if USB_PD_Support then
    baselib.report(0x00, "Begin power negotiation.")
    tcpclib.SetPermission(1, 2)
    PN_result = tcpclib.PresetPowerNegotiation(1, 500)  --timeout is 500ms
    if PN_result == LuaOk then
        baselib.report(0x00, "TD4.7.1.V.5 pass.")
    else
        if PN_result == LuaTimeout then
            baselib.report(0x00, "Power negotiation timeout.")
        else
            baselib.report(0x00, "Unsupported power request received.")
        end
        baselib.report(0x00, "TD4.7.1.V.5 fail.")
    end
end
-- 6.d步:Else if PUT supports USB 2.0 UFP:PUT starts data communications on its D+/D- pair. [TD.4.7.1.V.8]
-- 不会写,先空着
-- 6.e步:检查CC2电压不超过2.75V    (原文:For a PUT_R, VCONN is not supplied [TD.4.7.1.V.9] )
if busilib.getMonitorResult(monitor3) ~= 1 then        --出现1就是超过2750mV,否则检查点pass
    baselib.report(0x00, "TD.4.7.1.V.9 pass.")
else
    baselib.report(0x00, "TD.4.7.1.V.9 fail.")
end
-- 删除监控器
busilib.deleteMonitor(monitor3)
busilib.deleteTrans(trans1)
-- 第七步:CVS transmits Discover ID PD messaging for 200ms.
-- CVS发送Discover ID PD消息200ms
-- 之后会包装成函数,这里先空着
-- 第八步:CVS verifies that PUT:
-- 8.a步:If USB_PD_Support is YES:
-- 1、If USB_PD_Support is YES:Responds with valid GoodCRC messages. [TD.4.7.1.V.10]
if USB_PD_Support then
    -- 不会写,先空着
end
-- 2、If Responds_To_Discov_SOP is YES:Transmits valid Responder ACK messages. [TD.4.7.1.V.11]
if Responds_To_Discov_SOP then
    -- 不会写,先空着
end
-- 8.b步:Remains in Attached.SNK during and for 500ms after step 6. [TD.4.7.1.V.12]
local trans2 = busilib.newTrans4ad(8, ADC.CC1, {250, 1600})
local monitor2 = busilib.newMonitor(TMD.CHECK_POINT, trans2, {0}, {0}, {0})
baselib.delay(500)
if busilib.getMonitorResult(monitor2) ~= 1 then
    baselib.report(0x00, "TD4.7.1.V.12 fail.")
else
    baselib.report(0x00, "TD4.7.1.V.12 pass.")
end
-- 第九步:CVS transitions to Disabled
busilib.setUsbCcs(CCS.Disabled)
-- 第十步:CVS verifies that PUT transitions to Unattached.SNK before tVBUSOFF expires:PUT data communication has ceased. [TD.4.7.1.V.13]
-- .......

baselib.report(0x00, "Demo is done here.")
baselib.report(0x07, "\x04")
